/*

  The MIT License

  Copyright (c) 2008 - 2010. Dimarik

  Permission is hereby granted, free of charge, to any person obtaining a copy
  of this software and associated documentation files (the "Software"), to deal
  in the Software without restriction, including without limitation the rights
  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  copies of the Software, and to permit persons to whom the Software is
  furnished to do so, subject to the following conditions:

  The above copyright notice and this permission notice shall be included in
  all copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  THE SOFTWARE.

*/
package
{

import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.events.Event;

import ru.goodcore.composite.Branch;
import ru.goodcore.composite.CompositeEvent;
import ru.goodcore.composite.Cradle;
import ru.goodcore.composite.Leaf;
import ru.goodcore.core.QueryEvent;
import ru.goodcore.utils.ActivityListener;

/*
import ru.goodcore.core.composite.Branch;
*/

[SWF(frameRate='21',width='400',height='350',backgroundColor='0xD91189')]

public class TreeTest extends Sprite {

	/**
	 * Constructor 
	 */
	public function TreeTest() {
		super();		
		super.addEventListener(Event.ADDED_TO_STAGE, this.initialize);
	}

	/**
	 * @private
	 * @param event
	 */	
	private function initialize(event:Event):void {		
		super.stage.scaleMode = StageScaleMode.NO_SCALE;
		super.stage.align = StageAlign.TOP_LEFT;

		var performMeter:ActivityListener = new ActivityListener();
		super.stage.addChild(performMeter);

		this.startTest();
	}

	/**
	 * 
	 * 
	 */
	private function startTest():void {

		var cradle:		Cradle = new Cradle();
		cradle.addEventListener(CompositeEvent.ADDED,					this.handler_added);
		cradle.addEventListener(CompositeEvent.REMOVED,					this.handler_removed);
		cradle.addEventListener(QueryEvent.QUERY_RESULT,				this.handler_queryResult);
		cradle.addEventListener(QueryEvent.QUERY_RESULT,				this.handler_queryResultSecond);

		var branch1:	Branch = new Branch();
		branch1.name = 'branch1';
		branch1.addEventListener(CompositeEvent.ADDED,					this.handler_added);
		branch1.addEventListener(CompositeEvent.ADDED_TO_CRADLE,		this.handler_addedToStage);
		branch1.addEventListener(CompositeEvent.REMOVED,				this.handler_removed);
		branch1.addEventListener(CompositeEvent.REMOVED_FROM_CRADLE,	this.handler_removedFromStage);
		branch1.addEventListener(QueryEvent.QUERY_RESULT,				this.handler_queryResult);
		branch1.addEventListener(QueryEvent.QUERY_RESULT,				this.handler_queryResultSecond);

		var branch2:	Branch = new Branch();
		branch2.name = 'branch2';
		branch2.addEventListener(CompositeEvent.ADDED,					this.handler_added);
		branch2.addEventListener(CompositeEvent.ADDED_TO_CRADLE,		this.handler_addedToStage);
		branch2.addEventListener(CompositeEvent.REMOVED,				this.handler_removed);
		branch2.addEventListener(CompositeEvent.REMOVED_FROM_CRADLE,	this.handler_removedFromStage);
		branch2.addEventListener(QueryEvent.QUERY_RESULT,				this.handler_queryResult);
		branch2.addEventListener(QueryEvent.QUERY_RESULT,				this.handler_queryResultSecond);
		

		var branch3:	Branch = new Branch();
		branch3.name = 'branch3';
		branch3.addEventListener(CompositeEvent.ADDED,					this.handler_added);
		branch3.addEventListener(CompositeEvent.ADDED_TO_CRADLE,		this.handler_addedToStage);
		branch3.addEventListener(CompositeEvent.REMOVED,				this.handler_removed);
		branch3.addEventListener(CompositeEvent.REMOVED_FROM_CRADLE,	this.handler_removedFromStage);
		branch3.addEventListener(QueryEvent.QUERY_RESULT,				this.handler_queryResult);
		branch3.addEventListener(QueryEvent.QUERY_RESULT,				this.handler_queryResultSecond);

		var leaf1:		Leaf = new Leaf();
		leaf1.name = 'leaf1';
		//leaf1.addEventListener(CompositeEvent.ADDED,				this.handler_added);
		leaf1.addEventListener(CompositeEvent.ADDED_TO_CRADLE,		this.handler_addedToStage);
		leaf1.addEventListener(CompositeEvent.REMOVED,				this.handler_removed);
		leaf1.addEventListener(CompositeEvent.REMOVED_FROM_CRADLE,	this.handler_removedFromStage);
		leaf1.addEventListener(QueryEvent.QUERY_RESULT,				this.handler_queryResult);
		leaf1.addEventListener(QueryEvent.QUERY_RESULT,				this.handler_queryResultSecond);

		var leaf2:		Leaf = new Leaf();
		leaf2.name = 'leaf2';
		leaf2.addEventListener(CompositeEvent.ADDED,				this.handler_added);
		leaf2.addEventListener(CompositeEvent.ADDED_TO_CRADLE,		this.handler_addedToStage);
		leaf2.addEventListener(CompositeEvent.REMOVED,				this.handler_removed);
		leaf2.addEventListener(CompositeEvent.REMOVED_FROM_CRADLE,	this.handler_removedFromStage);
		leaf2.addEventListener(QueryEvent.QUERY_RESULT,				this.handler_queryResult);
		leaf2.addEventListener(QueryEvent.QUERY_RESULT,				this.handler_queryResultSecond);

		var leaf3:		Leaf = new Leaf();
		leaf3.name = 'leaf3';
		leaf3.addEventListener(CompositeEvent.ADDED,				this.handler_added);
		leaf3.addEventListener(CompositeEvent.ADDED_TO_CRADLE,		this.handler_addedToStage);
		leaf3.addEventListener(CompositeEvent.REMOVED,				this.handler_removed);
		leaf3.addEventListener(CompositeEvent.REMOVED_FROM_CRADLE,	this.handler_removedFromStage);
		leaf3.addEventListener(QueryEvent.QUERY_RESULT,				this.handler_queryResult);
		leaf3.addEventListener(QueryEvent.QUERY_RESULT,				this.handler_queryResultSecond);
		
		

		trace('=======   ADDED TEST  =================');

		trace('');
		trace('--- leaf3 add to branch3 ---');
		branch3.addChild(leaf3);
	

	
		trace('\n\n');
		trace('--- branch3 add to Cradle ---');
		cradle.addChild(branch3);
	

		trace('\n\n');
		trace('--- branch1 add to Cradle ---');
		cradle.addChild(branch1);
		trace('branch1.cradle', branch1.cradle);

		trace('\n\n');
		trace('--- branch2 add to branch1 ---');
		branch1.addChild(branch2);

		//return;

		trace('\n\n');
		trace('--- leaf1 add to branch2 ---');
		branch2.addChild(leaf1);

		trace('\n\n');
		trace('--- ', branch3, 'add to branch2 ---');
		branch2.addChild(branch3);

		trace('\n\n');
		trace('--- leaf2 add to branch2 ---');
		branch2.addChild(leaf2);
		trace('leaf2.cradle', leaf2.cradle);

		trace('\n\n');
		trace('==========  CHANGE PARENT TEST ==============');
		trace('--- leaf3 parent branch3 change to branch2 ---');
		branch2.addChild(leaf3);

		trace('\n\n');
		trace('==========  DISPATCH INHERITED EVENT TEST ==============');
		var queryEvent:QueryEvent = new QueryEvent({a:5}, true);
		trace('dispatchEvent result', leaf2.dispatchEvent( queryEvent ));

		trace('\n\n');
		trace('==========  REMOVED TEST ==============');
		trace('--- branch2 remove from branch1 ---');
		trace('branch2.cradle', branch2.cradle);
		branch1.removeChild(branch2);
		//branch1.removeChildAt(0);
		trace('branch2.cradle', branch2.cradle);
		trace('branch2.parent', branch2.parent);

		//branch2.addChildAt(branch2, 0);

		trace('\n\n');
		trace('==========  DISPATCH FOREING EVENT TEST ==============');
		//leaf3.dispatchEvent( new Event(Event.COMPLETE, true) );

	}

	private function handler_added(event:Event):void {
		trace('--- handler_added');
		trace('\t"target"', event.target);
		trace('\t\t"parent"', event.target.parent);
		trace('\t\t\t"cradle"', event.target.cradle);
		trace(event);

		if ( (event.target is Branch) && (event.target as Branch).numChildren ) {
			var child:Leaf = event.target.getChildAt(0);
			trace('child', child.name, 'cradle', child.cradle);
		}
		return;
		if (event.target.name == 'branch2') {
			if (event.target.parent) {
				trace('========== REMOVE ==========', event.target.name);
				event.target.parent.removeChild(event.target);
			}
		}
	}

	private function handler_removed(event:Event):void {
		trace('--- handler_removed');
		trace('\t"target"', event.target);
		trace('\t\t"parent"', event.target.parent);
		trace('\t\t\t"cradle"', event.target.cradle);
		trace(event);
	}

	private function handler_addedToStage(event:Event):void {
		trace('--- handler_addedToStage');
		trace('\t"target"', event.target);
		trace('\t\t"parent"', event.target.parent);
		trace('\t\t\t"cradle"', event.target.cradle);
		trace(event);
	}

	private function handler_removedFromStage(event:Event):void {
		trace('--- handler_removedFromStage');
		trace('\t"target"', event.target);
		trace('\t\t"parent"', event.target.parent);
		trace('\t\t\t"cradle"', event.target.cradle);
		trace(event);
	}

	private function handler_queryResult(event:QueryEvent):void {
		trace('1) First result', event.result, event);

		if (event.currentTarget is Branch) {
			//trace('>> stopPropagation');
			//event.stopPropagation();

			//trace('>> stopImmediatePropagation');
			//event.stopImmediatePropagation();
			trace('>> isDefaultPrevented', event.isDefaultPrevented());
			trace('>> preventDefault');
			event.preventDefault();
		}
	}

	private function handler_queryResultSecond(event:QueryEvent):void {
		trace('2) Second result', event.result, event);
	}

}
}